/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.widget;

import android.content.Context;
import android.database.Cursor;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;

import com.codeversed.plugins.buzz.R;
import com.codeversed.plugins.buzz.util.Constants;

public class CommentsAdapter extends ResourceCursorAdapter {
    
    public CommentsAdapter(Context context, Cursor cursor) {
        super(context, R.layout.list_item_comment, cursor, false);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        
        if (position % 2 == 1) {
            convertView.setBackgroundColor(0x20808080);
        } else {
            convertView.setBackgroundColor(0x00FFFFFF);
        }
        
        return convertView;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cur) {
        ViewHolder holder = (ViewHolder) view.getTag();
        
        if (holder == null) {
            holder = (ViewHolder) newView(context, cur, (ViewGroup) view.getParent()).getTag();
        }
        
        holder.authorView.setText(cur.getString(cur.getColumnIndex(Constants.FIELD_AUTHOR)));
        holder.commentView.setText(Html.fromHtml(cur.getString(cur.getColumnIndex(Constants.FIELD_COMMENT))));
        
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy h:mm a");
        long timeStamp = cur.getLong(cur.getColumnIndex(Constants.FIELD_POSTED));
        String date = format.format(timeStamp);
        
        holder.postDateView.setText(date);
    }
    
    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        View view = super.newView(context, cur, parent);
        
        holder.authorView = (TextView) view.findViewById(R.id.list_comment_author);
        holder.commentView = (TextView) view.findViewById(R.id.list_comment_content);
        holder.postDateView = (TextView) view.findViewById(R.id.list_comment_posted);

        holder.commentView.setMovementMethod(LinkMovementMethod.getInstance());

        view.setTag(holder);
        return view;
    }
    
    static class ViewHolder {
        TextView authorView;
        TextView commentView;
        TextView postDateView;
    }
}
